package com.itheima;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class MyMeta implements MetaObjectHandler {

    @Autowired
    private HttpSession httpSession;

    //@TableField(fill = FieldFill.INSERT_UPDATE)
    //有这些注解的字段会在下面的两个方法中对应赋值

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("得到的数据是：{}",httpSession.getAttribute("employee"));
        metaObject.setValue("createTime",new Date());
        metaObject.setValue("createUser",httpSession.getAttribute("employee"));
        metaObject.setValue("updateTime",new Date());
        metaObject.setValue("updateUser",httpSession.getAttribute("employee"));
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("得到的数据是：{}",httpSession.getAttribute("employee"));
        metaObject.setValue("updateTime",new Date());
        metaObject.setValue("updateUser",httpSession.getAttribute("employee"));
    }
}
